package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.domain.Address;
import com.qf.domain.Order;
import com.qf.domain.User;
import com.qf.utils.DataSourceUtils;
import com.qf.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/20 22:39
 * description:
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public Long findCountByAid(String aid) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select count(0) from tb_order where aid=? ";
        try {
            Long count=(Long) queryRunner.query(sql, new ScalarHandler(),aid);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return Long.valueOf(0);
    }

    @Override
    public void insert(Order order) {
        Connection conn = TxUtils.getConn();
        QueryRunner queryRunner=new QueryRunner();
        String sql=" insert into tb_order (id,uid,money,status,time,aid) values(?,?,?,?,?,?)";
        try {
            queryRunner.update(conn,sql,order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paySuccess(Order order) {
        Connection conn = TxUtils.getConn();
        QueryRunner queryRunner=new QueryRunner();
        String sql=" update tb_order set status=? where id=? ";
        try {
            queryRunner.update(conn,sql,order.getStatus(),order.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Order> selectAllOrder() {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,uid,money,status,time,aid from tb_order";
        try {
            List<Order> orderList = queryRunner.query(sql, new BeanListHandler<>(Order.class));
            return orderList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void changestatus(String oid) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" update tb_order set status=3 where id=? ";
        try {
            queryRunner.update(sql,oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Order searchOrder(String where, List<Object> params,int uid) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,uid,money,status,time,aid from tb_order "+where+"and uid="+uid+" ";
        try {
            Order order=queryRunner.query(sql,new BeanHandler<>(Order.class),params.get(0));
            return order;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Order> searchOrder(String where, List<Object> params) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,uid,money,status,time,aid from tb_order "+where;
        try {
            List<Order> orderList=queryRunner.query(sql,new BeanListHandler<>(Order.class),params.toArray());
            return orderList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Order> searchOrderByUid(int id) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,uid,money,status,time,aid from tb_order where uid=?  ";
        try {
            List<Order> orders=queryRunner.query(sql,new BeanListHandler<>(Order.class),id);
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Order> searchOrderByStatus(String status) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,uid,money,status,time,aid from tb_order where status=?  ";
        try {
            List<Order> orders=queryRunner.query(sql,new BeanListHandler<>(Order.class),status);
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Order> searchOrderByUidAndStatus(int id, String status) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,uid,money,status,time,aid from tb_order where uid=? and status=? ";
        try {
            List<Order> orders=queryRunner.query(sql,new BeanListHandler<>(Order.class),id,status);
            return orders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
